package com.ddf.publics.entity;

/**
 * Created by Zero on 2020-4-27 11:16
 */

public class Page {
    private Integer allCount;//总行数
    private Integer count;//每页行数

    private Integer page;//当前页数
    private Integer pageAll;//总页数
    private Integer limit;//分页开始的行数

    //获取分页开始的行数
    public Integer getLimit() {
        //当总页数为0的时候limit为0
        return allCount==0?0:(this.getPage()-1)*this.count;
    }

    public Integer getPageAll() {//获取总页数
        //抛异常，请先设置总页数
        if(allCount==null||count==null){
            throw new RuntimeException("请先设置完总行数(allCount)和每页行数(count)才能获取其他页数和行数!");
        }
        return (int)Math.ceil(((float)allCount/(float)count));
    }

    //当前页数
    public Integer getPage() {
        if(page==null){
            page = 1;
        }
        //当前页数不能为
        if(page<1){
            return 1;
        }

        //也不能大于总页数
        if(page>this.getPageAll()){
            return getPageAll();
        }

        return page;
    }



    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getCount() {
        if(count==0||count==null){
            count=1;
        }
        if(count<0){
            throw new RuntimeException("当前页数(count)不能小于0!");
        }
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getAllCount() {
        return allCount;
    }

    public void setAllCount(Integer allCount) {
        if(allCount<0){
            throw new RuntimeException("总行数(allCount)不能小于0!");
        }
        this.allCount = allCount;
    }

    @Override
    public String toString() {
        return "Page{" +
                "page=" + this.getPage() +
                ", pageAll=" + this.getPageAll() +
                ", count=" + this.getCount() +
                ", allCount=" + this.getAllCount() +
                ", limit=" + this.getLimit() +
                '}';
    }
}
